package se.citerus.dddsample.infrastructure.messaging.jms;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import se.citerus.dddsample.application.CargoInspectionService;
import se.citerus.dddsample.domain.model.cargo.TrackingId;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * Consumes JMS messages and delegates notification of misdirected cargo to the
 * tracking service.
 * 
 * This is a programmatic hook into the JMS infrastructure to make cargo
 * inspection message-driven.
 */
public class CargoHandledConsumer implements MessageListener {

	private CargoInspectionService cargoInspectionService;
	private final Log logger = LogFactory.getLog(getClass());

	@Override
	public void onMessage(final Message message) {
		try {
			final TextMessage textMessage = (TextMessage) message;
			final String trackingidString = textMessage.getText();

			cargoInspectionService.inspectCargo(new TrackingId(trackingidString));
		} catch (Exception e) {
			logger.error(e, e);
		}
	}

	public void setCargoInspectionService(CargoInspectionService cargoInspectionService) {
		this.cargoInspectionService = cargoInspectionService;
	}
}
